package GUI;

import java.awt.HeadlessException;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import Client_logic.ClientNotInitializesException;
import Client_logic.ConnectionToServer;

/**
 * This a main class for MaketDepardment, all process for  MaketDepardment
 * starts from this class
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamil
 */
public class MaketDepardmentForm extends ClientAbstractFORM
{
	
	private static final long serialVersionUID = -4117657374226987002L;
	private JButton addProductButton = null;
	private JButton updateProductButton = null;
	private JButton removeProductButton = null;
	private JButton createCampaignButton = null;
	private JButton updateCampaignButton = null;
	private JButton activityTrackingButton = null;
	private JButton requestAuthorizationButton = null;	
		
	/**
	 * This is the default constructor
	 */
	public MaketDepardmentForm() 
	{
		super();
		initialize();	
	}
	
	/**
	 * This method initializes MaketDepardmentForm
	 */
	private void initialize() 
	{
		this.setSize(500, 500);
		this.setLayout(null);
		this.add(getAddProductButton(),null);
		this.add(getUpdateProductButton(),null);
		this.add(getRemoveProductButton(),null);
		this.add(getCreateCampaignButton(),null);
		this.add(getActivityTrackingButton(),null);
		this.add(getRequestAuthorizationButton(),null);
		this.add(getUpdateCampaignButton(),null);
	}
	
	/**
	 * This method initializes addProductButton	
	 */
	private JButton getAddProductButton() 
	{
		if (addProductButton == null) 
		{
			addProductButton = new JButton();
			addProductButton.setBounds(new Rectangle(20, 20, 172, 29));
			addProductButton.setText("Add Product");
			addProductButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{			
					try{
						JFrame jframe = new JFrame();
						ProductForm productform = new ProductForm();
						jframe.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
						jframe.setSize( 400, 300 ); 
						jframe.setTitle("Add Product");
						productform.addSaveButton();
						productform.getcancelButton().addActionListener(new java.awt.event.ActionListener() {
							public void actionPerformed(java.awt.event.ActionEvent e) {						
							}
						});		
						jframe.setContentPane(productform);
						jframe.setVisible( true );
					}
					catch(HeadlessException headlessexception){
						JOptionPane.showMessageDialog(null, "FATAL ERROR");
					}
				}
			});
		}
		return addProductButton;
	}
	
	/**
	 * This method initializes updateProductButton
	 */
	private JButton getUpdateProductButton() 
	{
		if (updateProductButton == null) 
		{
			updateProductButton = new JButton();
			updateProductButton.setBounds(new Rectangle(20, 50, 172, 29));
			updateProductButton.setText("Update product");
			updateProductButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{			
					try{
						ProductSearch productsearch = new ProductSearch("UpdateProduct");
						productsearch.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE);
						productsearch.setSize( 400, 300 );
						productsearch.setVisible( true );
					}
					catch(HeadlessException headlessexception){
						JOptionPane.showMessageDialog(null, "FATAL ERROR");
					}	
				}
			});
		}
		return updateProductButton;
	}
	
	
	/**
	 * This method initializes removeProductButton
	 */
	private JButton getRemoveProductButton() 
	{
		if (removeProductButton == null) 
		{
			removeProductButton = new JButton();
			removeProductButton.setBounds(new Rectangle(20, 80, 172, 29));
			removeProductButton.setText("Remove product");
			removeProductButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{			
					try{
						ProductSearch productsearch = new ProductSearch("removeProduct");
						productsearch.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE);
						productsearch.setSize( 400, 300 ); 
						productsearch.setVisible( true ); 
					}
					catch(HeadlessException headlessexception){
						JOptionPane.showMessageDialog(null, "FATAL ERROR");
					}	
				}
			});
		}
		return removeProductButton;
	}
	
	/**
	 * This method initializes createCampaignButton
	 */
	private JButton getCreateCampaignButton() 
	{
		if (createCampaignButton == null) 
		{
			createCampaignButton = new JButton();
			createCampaignButton.setBounds(new Rectangle(20, 110, 172, 29));
			createCampaignButton.setText("Create Campaign");
			createCampaignButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{			
					try
					{
						ProductSearch productsearch = new ProductSearch("CreateCampaign");
						productsearch.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE);
						productsearch.setSize( 520, 300 ); // set frame size
						productsearch.setVisible( true ); // display frame
					}catch(HeadlessException headlessexception)
					{
						JOptionPane.showMessageDialog(null, "FATAL ERROR","",JOptionPane.ERROR_MESSAGE);
					}	
				}
			});
		}
		return createCampaignButton;
	}
	
	/**
	 * This method initializes createCampaignButton
	 */
	private JButton getUpdateCampaignButton() 
	{
		if (updateCampaignButton == null) 
		{
			updateCampaignButton = new JButton();
			updateCampaignButton.setBounds(new Rectangle(20, 140, 172, 29));
			updateCampaignButton.setText("Update Campaign");
			updateCampaignButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{			
					try
					{						
						TemplateSearch templatesearch = new TemplateSearch("UpdateTemplate");
						templatesearch.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE);
						templatesearch.setSize( 520, 300 ); // set frame size
						templatesearch.setVisible( true ); // display frame
					}catch(HeadlessException headlessexception)
					{
						JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);
					}	
				}
			});
		}
		return updateCampaignButton;
	}
	
	/**
	 * This method initializes activityTrackingButton
	 */
	private JButton getActivityTrackingButton() 
	{
		if (activityTrackingButton  == null) 
		{
			activityTrackingButton  = new JButton();
			activityTrackingButton .setBounds(new Rectangle(20, 170, 172, 29));
			activityTrackingButton .setText("Activity tracking");
			activityTrackingButton .addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{			
					try 
					{
						ConnectionToServer connectionToServer = new ConnectionToServer();
						String str = (String)connectionToServer.analyzesSystem();
						if (str != null)
						{
							if (str.equals("OK"))
								JOptionPane.showMessageDialog(null, "Analyzes succsess");
							else
								JOptionPane.showMessageDialog(null, "ERROR!!!","",JOptionPane.ERROR_MESSAGE);
						}
					} catch (ClientNotInitializesException e1) 
					{
						JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);
					}
					
				}
			});
		}
		return activityTrackingButton ;
	}
	
	/**
	 * This method initializes requestAuthorizationButton
	 */
	private JButton getRequestAuthorizationButton() 
	{
		if (requestAuthorizationButton  == null) 
		{
			requestAuthorizationButton  = new JButton();
			requestAuthorizationButton .setBounds(new Rectangle(20, 200, 172, 29));
			requestAuthorizationButton .setText("Request Authorization");
			requestAuthorizationButton .addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{			
					try
					{
						CustomerSearch customersearch = new CustomerSearch("RequestAuthorization");
						customersearch.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE);
						customersearch.setSize(300, 150); // set frame size
						customersearch.setVisible( true ); // display frame
					}catch(HeadlessException headlessexception)
					{
						JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);
					}	
				}
			});
		}
		return requestAuthorizationButton;
	}
	
}
